package cn.com.aiouyi.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Created by tommy on 2018/4/12.
 */
public class ExceptionHelper {
    private ExceptionHelper(){}

    public static String getThrowableStackTrace(Throwable t){
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            t.printStackTrace(pw);
            pw.flush();
            sw.flush();
        } finally {
            if (sw != null){
                try {
                    sw.close();
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
            if (pw != null){
                pw.close();
            }
        }
        return sw.toString();
    }

    public static String getExceptionStackTrace(Exception t){
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            t.printStackTrace(pw);
            pw.flush();
            sw.flush();
        } finally {
            if (sw != null){
                try {
                    sw.close();
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
            if (pw != null){
                pw.close();
            }
        }
        return sw.toString();
    }
}
